// packageH/sign_shopV2/component/signControl/signControl.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    datas: {
      type: Object,
      value: {}
    },
    cid: {
      type: [Boolean, String],
      value: '0'
    },
    scene: {
      type: [Boolean, String],
      value: '0'
    },
    order_id: {
      type: [String],
      value: '0'
    },
  },
  observers: {
    'show': function (valA, valB) {
      this.setData({
        showControl: valA,
        form_data: this.formatterForm()
      })
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    showControl: false,
    showDatePicker: false,
    minDate: new Date(2020, 0, 1).getTime(),
    currentDate: new Date().getTime(),
    currentIndex: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    formatterForm(){
      let list = this.properties.datas.form_data||[];
      list.forEach((item,i) => {
        if(item.fill_type == 1 &&( item.form_type == 4|| item.form_type ==5|| item.form_type == 6)) {
          list[i].form_value = ''
        } 
      });
      return list
    },
    hiddenPop() {
      this.setData({
        showControl: false,
        form_data: []
      })
    },
    handlerInput(event) {
      let index = event.target.dataset.index;
      let type = event.target.dataset.type;
      let value = event.detail;
      if(type == 'number'){
        value = event.detail.replace(/[^0-9]/gi, "");
      }
      if(type == 'card'){
        value = event.detail.replace(/[^\d|xX]/g, '');
      }
      this.setData({
        ['form_data[' + index + '].form_value']: value
      });
    },
    toSign() {
      let _AllSet = true;
      let _text = '';
      this.data.form_data.forEach(item => {
        if (item.fill_type == 1 && item.form_value == '') {
          _AllSet = false; //有未填写的控件
          _text = '请填写完整数据！';
        }
        if(item.verify) {
          _AllSet = false;//有未填写的控件
          _text = '请输入18位身份证号';
        }
      });
      if (!_AllSet) return app.tips(_text);
      console.log(this.data.form_data)
      this.toSignPage();
    },
    toSignPage() {
      let _url = '';
      let json = {
        scene_id: this.properties.datas.id,
        cid: this.properties.cid,
        form_data: this.data.form_data
      };
      if (this.properties.scene == 1) {
        _url = 'plugin.shop-esign-v2.frontend.controllers.index.order';
        json.order_id = this.properties.order_id;
      }
      if (this.properties.scene == 2) _url = 'plugin.shop-esign-v2.frontend.controllers.index.withdraw';
      if (this.properties.scene == 3) _url = 'plugin.shop-esign-v2.frontend.controllers.index.loveChange';
      let queryUrl = app.getNetAddresss(_url);
      app._postNetWork({
        url: queryUrl,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result === 1) {
            this.hiddenPop();
            wx.navigateTo({
              url: `/packageH/signPage/signPage?id=${res.data.contract_id}&from=signV2&scene=` + this.properties.scene + '&cid=' + this.properties.cid + "&oid=" + this.properties.order_id||0,
            });
          } else {
            wx.showModal({
              title: "提示",
              content: res.msg,
              showCancel: false
            });
          }
        },
        fail(err) {
          console.log(err, "----失败-----");
        }
      });
    },
    showDatePickerHandle(event) {
      let index = event.target.dataset.index;
      this.setData({
        showDatePicker: true,
        currentIndex: index
      })
    },
    hiddenDatePickerHandle() {
      this.setData({
        showDatePicker: false
      })
    },
    confirmDatePickerHandle(e) {
      let time = e.detail;
      let changtime = this.timestampToTime(time);
      this.setData({
        showDatePicker: false,
        ['form_data[' + this.data.currentIndex + '].form_value']: changtime
      })
    },
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var D = (date.getDate() + 1 < 10 ? '0' + (date.getDate() + 1) : date.getDate() + 1);
      return Y + M + D;
    },
    verifyCardLength(e){
      let i = e.currentTarget.dataset.index;
      let detail = e.detail;
      this.data.form_data[i].verify = detail.length < 18? true: false;
    }
  }
})